#include "stdafx.h"
#include "WCPeer.h"

WCPeer::WCPeer(int port)
{
	SOCKET zsocket;

	sockaddr_in local;
	local.sin_family=AF_INET;
	local.sin_port=htons(port);
	local.sin_addr.s_addr=INADDR_ANY;

	zsocket=socket(AF_INET,SOCK_DGRAM,0);
	//int optval = 1;
	//setsockopt(zsocket, SOL_zsocket, SO_BROADCAST, (char*)&optval, sizeof(optval));
	bind(zsocket,(struct sockaddr*)&local,sizeof local);
	m_udpReader = new UDPReader(zsocket);
	m_udpWriter = new UDPWriter(zsocket);
}

WCPeer::~WCPeer()
{
	delete m_udpReader;
	m_udpReader = NULL;
	delete m_udpWriter;
	m_udpWriter = NULL;
}

int WCPeer::send(const char* ip, int port, const char* message, int len)
{
	return m_udpWriter->write(message, len, ip, port);
}


int WCPeer::receive(char* ip, int* port, char* message, int len)
{
	return m_udpReader->read(message, len, ip, port);
}